package com.axdoc.walletbiz.record.service.impl;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.axdoc.common.constant.WalletConstant;
import com.axdoc.wallet.exceptions.WalletBizException;
import com.axdoc.wallet.service.BizWalletService;
import com.axdoc.walletbiz.record.dao.IOrderDao;
import com.axdoc.walletbiz.record.dao.IOrderRefundDao;
import com.axdoc.walletbiz.record.entity.PayOrder;
import com.axdoc.walletbiz.record.entity.PayOrderRefund;
import com.axdoc.walletbiz.record.exceptions.WalletBizRecordException;
import com.axdoc.walletbiz.record.mq.message.TransactionDoneMessage;
import com.axdoc.walletbiz.record.service.RecordService;

@Service("recordService")
public class RecordServiceImpl implements RecordService {
	public static Logger logger = LoggerFactory.getLogger("record-paySuccess");

	@Autowired
	private IOrderDao orderDao;

	@Autowired
	private IOrderRefundDao orderRefundDao;

	@Autowired
	private BizWalletService bizWalletService;

	@Override
	public void paySuccess(String trade_no, Long biz_time, String msgId) throws WalletBizRecordException {
		try {
			if (StringUtils.isEmpty(trade_no)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数trade_no");
			}
			PayOrder payOrder = orderDao.query(trade_no);
			if (payOrder == null) {
				throw new WalletBizRecordException(WalletBizRecordException.NOT_EXSIT, "支付订单不存在");
			}
			// 分析订单金额组成
			String aid = payOrder.getAid(); // 用户aid
			BigDecimal totalFee = payOrder.getTotal_fee(); // 实际需要支付现金金额
			BigDecimal cashFee = payOrder.getCash_fee(); // 实际已经支付现金金额
			BigDecimal couponFee = payOrder.getCoupon_fee();// 券优惠金额
			BigDecimal saleFee = payOrder.getSale_fee();// 促销、活动优惠金额
			BigDecimal vipFee = payOrder.getVip_sale_fee();// vip优惠金额
			// 判断订单状态是否是支付完成（2）TODO
			// 组合记账需求
			JSONObject require = new JSONObject();
			// 平台记账
			JSONArray platform = new JSONArray();
			// 患者端记账
			JSONObject patient = new JSONObject();
			JSONArray record = new JSONArray();
			if (totalFee != null && cashFee != null && cashFee.compareTo(new BigDecimal(0)) != 0) {
				// 用户账户生成两条流水，充值，付款 ,cashFee中有鼓励金，采用totalFee记账流水
				JSONObject recharge = new JSONObject();
				recharge.put("type", WalletConstant.WALLET_TYPE_BALANCE);
				recharge.put("trade_no", trade_no);
				recharge.put("trx_no", payOrder.getTransaction_id());
				recharge.put("biz_no", payOrder.getBizid());
				recharge.put("change_amount", totalFee.multiply(new BigDecimal(100)).intValue());
				recharge.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				recharge.put("trx_code", WalletConstant.PATIENT_RECORD_TYPE_RECHARGE);
				// recharge.put("biz_code", );
				recharge.put("uid", payOrder.getUid());
				recharge.put("channel_id", payOrder.getChannel());
				record.add(recharge);
				JSONObject pay = new JSONObject();
				pay.put("type", WalletConstant.WALLET_TYPE_BALANCE);
				pay.put("trade_no", trade_no);
				pay.put("trx_no", payOrder.getTransaction_id());
				pay.put("biz_no", payOrder.getBizid());
				pay.put("change_amount", totalFee.multiply(new BigDecimal(100)).intValue());
				pay.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
				pay.put("trx_code", WalletConstant.PATIENT_RECORD_TYPE_RECHARGE);
				// recharge.put("biz_code", );
				pay.put("uid", payOrder.getUid());
				pay.put("channel_id", payOrder.getChannel());
				record.add(pay);
				// 平台账户生成一条入账流水
				JSONObject inacc = new JSONObject();
				inacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
				inacc.put("trade_no", trade_no);
				inacc.put("trx_no", payOrder.getTransaction_id());
				inacc.put("biz_no", payOrder.getBizid());
				inacc.put("change_amount", totalFee.multiply(new BigDecimal(100)).intValue());
				inacc.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				inacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_IN);
				// inacc.put("biz_code", );
				inacc.put("uid", payOrder.getUid());
				inacc.put("channel_id", payOrder.getChannel());
				platform.add(inacc);
			}
			if (couponFee != null && couponFee.compareTo(new BigDecimal(0)) != 0) {
				// 券子账户生成两条流水，充值，付款
				JSONObject recharge = new JSONObject();
				recharge.put("type", WalletConstant.WALLET_TYPE_COUPON);
				recharge.put("trade_no", trade_no);
				recharge.put("trx_no", payOrder.getTransaction_id());
				recharge.put("biz_no", payOrder.getBizid());
				recharge.put("coupon_amount", couponFee.multiply(new BigDecimal(100)).intValue());
				recharge.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				recharge.put("uid", payOrder.getUid());
				recharge.put("channel_id", payOrder.getChannel());
				recharge.put("coupon_id", payOrder.getCoupon_id());
				record.add(recharge);
				JSONObject pay = new JSONObject();
				pay.put("type", WalletConstant.WALLET_TYPE_COUPON);
				pay.put("trade_no", trade_no);
				pay.put("trx_no", payOrder.getTransaction_id());
				pay.put("biz_no", payOrder.getBizid());
				pay.put("coupon_amount", couponFee.multiply(new BigDecimal(100)).intValue());
				pay.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
				pay.put("uid", payOrder.getUid());
				pay.put("channel_id", payOrder.getChannel());
				pay.put("coupon_id", payOrder.getCoupon_id());
				record.add(pay);
				// 平台账户生成一条入账流水
				JSONObject inacc = new JSONObject();
				inacc.put("type", WalletConstant.WALLET_TYPE_COUPON);
				inacc.put("trade_no", trade_no);
				inacc.put("trx_no", payOrder.getTransaction_id());
				inacc.put("biz_no", payOrder.getBizid());
				inacc.put("change_amount", couponFee.multiply(new BigDecimal(100)).intValue());
				inacc.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				inacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_IN);
				// inacc.put("biz_code", );
				inacc.put("uid", payOrder.getUid());
				inacc.put("channel_id", payOrder.getChannel());
				inacc.put("coupon_id", payOrder.getCoupon_id());
				platform.add(inacc);
			}
			if (vipFee != null && vipFee.compareTo(new BigDecimal(0)) != 0) {
				// vip子账户生成两条流水，充值，付款
				JSONObject recharge = new JSONObject();
				recharge.put("type", WalletConstant.WALLET_TYPE_VIP);
				recharge.put("trade_no", trade_no);
				recharge.put("trx_no", payOrder.getTransaction_id());
				recharge.put("biz_no", payOrder.getBizid());
				recharge.put("service_amount", vipFee.multiply(new BigDecimal(100)).intValue());
				recharge.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				recharge.put("uid", payOrder.getUid());
				recharge.put("channel_id", payOrder.getChannel());
				recharge.put("service_id", payOrder.getVip_sale_id());
				record.add(recharge);
				JSONObject pay = new JSONObject();
				pay.put("type", WalletConstant.WALLET_TYPE_VIP);
				pay.put("trade_no", trade_no);
				pay.put("trx_no", payOrder.getTransaction_id());
				pay.put("biz_no", payOrder.getBizid());
				pay.put("service_amount", vipFee.multiply(new BigDecimal(100)).intValue());
				pay.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
				pay.put("uid", payOrder.getUid());
				pay.put("channel_id", payOrder.getChannel());
				pay.put("service_id", payOrder.getVip_sale_id());
				record.add(pay);
				// 平台账户生成一条入账流水
				JSONObject inacc = new JSONObject();
				inacc.put("type", WalletConstant.WALLET_TYPE_VIP);
				inacc.put("trade_no", trade_no);
				inacc.put("trx_no", payOrder.getTransaction_id());
				inacc.put("biz_no", payOrder.getBizid());
				inacc.put("change_amount", vipFee.multiply(new BigDecimal(100)).intValue());
				inacc.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				inacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_IN);
				// inacc.put("biz_code", );
				inacc.put("uid", payOrder.getUid());
				inacc.put("channel_id", payOrder.getChannel());
				inacc.put("vip_service_id", payOrder.getVip_sale_id());
				platform.add(inacc);
			}
			if (saleFee != null && saleFee.compareTo(new BigDecimal(0)) != 0) {
				// 活动子账户生成两条流水，充值，付款
				JSONObject recharge = new JSONObject();
				recharge.put("type", WalletConstant.WALLET_TYPE_ACTIVITY);
				recharge.put("trade_no", trade_no);
				recharge.put("trx_no", payOrder.getTransaction_id());
				recharge.put("biz_no", payOrder.getBizid());
				recharge.put("activity_amount", saleFee.multiply(new BigDecimal(100)).intValue());
				recharge.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				recharge.put("uid", payOrder.getUid());
				recharge.put("channel_id", payOrder.getChannel());
				recharge.put("activity_id", payOrder.getSale_id());
				record.add(recharge);
				JSONObject pay = new JSONObject();
				pay.put("type", WalletConstant.WALLET_TYPE_VIP);
				pay.put("trade_no", trade_no);
				pay.put("trx_no", payOrder.getTransaction_id());
				pay.put("biz_no", payOrder.getBizid());
				pay.put("activity_amount", saleFee.multiply(new BigDecimal(100)).intValue());
				pay.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
				pay.put("uid", payOrder.getUid());
				pay.put("channel_id", payOrder.getChannel());
				pay.put("activity_id", payOrder.getSale_id());
				record.add(pay);
				// 平台账户生成一条入账流水
				JSONObject inacc = new JSONObject();
				inacc.put("type", WalletConstant.WALLET_TYPE_ACTIVITY);
				inacc.put("trade_no", trade_no);
				inacc.put("trx_no", payOrder.getTransaction_id());
				inacc.put("biz_no", payOrder.getBizid());
				inacc.put("change_amount", saleFee.multiply(new BigDecimal(100)).intValue());
				inacc.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				inacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_IN);
				// inacc.put("biz_code", );
				inacc.put("uid", payOrder.getUid());
				inacc.put("channel_id", payOrder.getChannel());
				inacc.put("activity_id", payOrder.getSale_id());
				platform.add(inacc);
			}
			patient.put("aid", aid);
			patient.put("records", record);
			require.put("patient", patient);
			require.put("platform", platform);
			require.put("msgId", msgId);
			require.put("biz_time", biz_time);
			// 调用账户服务-事务记账
			bizWalletService.WalletRecordPaySuccess(require.toJSONString(), String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("支付成功记账-分析订单金额组成异常", e);
		}
	}

	@Override
	public void refundSuccess(String trade_no, Long biz_time, String msgId) throws WalletBizRecordException {
		try {
			if (StringUtils.isEmpty(trade_no)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数trade_no");
			}
			PayOrder payOrder = orderDao.query(trade_no);
			if (payOrder == null) {
				throw new WalletBizRecordException(WalletBizRecordException.NOT_EXSIT, "支付订单不存在");
			}
			List<PayOrderRefund> list = orderRefundDao.queryByTradeNo(trade_no);
			if (list == null || list.size() == 0) {
				throw new WalletBizRecordException(WalletBizRecordException.NOT_EXSIT, "退款订单不存在");
			}
			// 分析订单金额组成
			String aid = payOrder.getAid(); // 用户aid
			BigDecimal couponFee = payOrder.getCoupon_fee();// 券优惠金额
			// 组合记账需求
			JSONObject require = new JSONObject();
			// 平台记账
			JSONArray platform = new JSONArray();
			// 患者端记账
			JSONObject patient = new JSONObject();
			JSONArray record = new JSONArray();
			// 余额子账户记流水账
			for (PayOrderRefund por : list) {
				BigDecimal refundFee = por.getRefund_fee();
				if (refundFee != null && refundFee.compareTo(new BigDecimal(0)) != 0) {
					// 平台账户生成一条出账流水-退款
					JSONObject outacc = new JSONObject();
					outacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
					outacc.put("trade_no", trade_no);
					outacc.put("trx_no", payOrder.getTransaction_id());
					outacc.put("biz_no", payOrder.getBizid());
					outacc.put("change_amount", refundFee.intValue());
					outacc.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
					outacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_OUT);
					// outacc.put("biz_code", );
					outacc.put("uid", payOrder.getUid());
					outacc.put("channel_id", payOrder.getChannel());
					platform.add(outacc);

					// 用户账户生成两条流水，退款，转出
					JSONObject refund = new JSONObject();
					refund.put("type", WalletConstant.WALLET_TYPE_BALANCE);
					refund.put("trade_no", trade_no);
					refund.put("trx_no", payOrder.getTransaction_id());
					refund.put("biz_no", payOrder.getBizid());
					refund.put("change_amount", refundFee.intValue());
					refund.put("change_direction", WalletConstant.WALLET_DIRET_IN);
					refund.put("trx_code", WalletConstant.PATIENT_RECORD_TYPE_REFUND);
					// recharge.put("biz_code", );
					refund.put("uid", payOrder.getUid());
					refund.put("channel_id", payOrder.getChannel());
					record.add(refund);
					JSONObject transferout = new JSONObject();
					transferout.put("type", WalletConstant.WALLET_TYPE_BALANCE);
					transferout.put("trade_no", trade_no);
					transferout.put("trx_no", payOrder.getTransaction_id());
					transferout.put("biz_no", payOrder.getBizid());
					transferout.put("change_amount", refundFee.intValue());
					transferout.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
					transferout.put("trx_code", WalletConstant.PATIENT_RECORD_TYPE_TRANSFEROUT);
					// recharge.put("biz_code", );
					transferout.put("uid", payOrder.getUid());
					transferout.put("channel_id", payOrder.getChannel());
					record.add(transferout);
				}
			}
			// 券子账户记流水账
			// 判断业务类型，是否需要退券，如需要退券，平台和患者都记退券流水，如不需要则不记账，目前都记退券流水账 TODO
			// vip账户与活动账户，平台和患者都不记流水，因为是服务，无实体
			if (couponFee != null && couponFee.compareTo(new BigDecimal(0)) != 0) {
				// 平台账户生成一条券出账流水
				JSONObject outacc = new JSONObject();
				outacc.put("type", WalletConstant.WALLET_TYPE_COUPON);
				outacc.put("trade_no", trade_no);
				outacc.put("trx_no", payOrder.getTransaction_id());
				outacc.put("biz_no", payOrder.getBizid());
				outacc.put("change_amount", couponFee.multiply(new BigDecimal(100)).intValue());
				outacc.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				outacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_OUT);
				// inacc.put("biz_code", );
				outacc.put("uid", payOrder.getUid());
				outacc.put("channel_id", payOrder.getChannel());
				outacc.put("coupon_id", payOrder.getCoupon_id());
				platform.add(outacc);
				// 券子账户生成两条流水，退款，转出
				JSONObject refund = new JSONObject();
				refund.put("type", WalletConstant.WALLET_TYPE_COUPON);
				refund.put("trade_no", trade_no);
				refund.put("trx_no", payOrder.getTransaction_id());
				refund.put("biz_no", payOrder.getBizid());
				refund.put("coupon_amount", couponFee.multiply(new BigDecimal(100)).intValue());
				refund.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				refund.put("uid", payOrder.getUid());
				refund.put("channel_id", payOrder.getChannel());
				refund.put("coupon_id", payOrder.getCoupon_id());
				record.add(refund);
				JSONObject teansferout = new JSONObject();
				teansferout.put("type", WalletConstant.WALLET_TYPE_COUPON);
				teansferout.put("trade_no", trade_no);
				teansferout.put("trx_no", payOrder.getTransaction_id());
				teansferout.put("biz_no", payOrder.getBizid());
				teansferout.put("coupon_amount", couponFee.multiply(new BigDecimal(100)).intValue());
				teansferout.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
				teansferout.put("uid", payOrder.getUid());
				teansferout.put("channel_id", payOrder.getChannel());
				teansferout.put("coupon_id", payOrder.getCoupon_id());
				record.add(teansferout);
			}
			patient.put("aid", aid);
			patient.put("records", record);
			require.put("patient", patient);
			require.put("platform", platform);
			require.put("msgId", msgId);
			require.put("biz_time", biz_time);
			// 调用账户服务-事务记账
			bizWalletService.WalletRecordRefundSuccess(require.toJSONString(), String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("退款成功记账-分析订单金额组成异常", e);
		}
	}

	@Override
	public void risk(String biz_no, String doc_id, int balance, Long biz_time, String biz_type, String income_id,
			String trade_no, String msgId) throws WalletBizRecordException {
		try {
			if (StringUtils.isEmpty(doc_id)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数doc_id");
			}
			// 组合记账需求
			JSONObject require = new JSONObject();
			// 平台记账
			JSONArray platform = new JSONArray();
			// 医生端记账
			JSONObject doctor = new JSONObject();
			JSONArray record = new JSONArray();
			// 医生账户生成一条流水，风控扣款/加款
			JSONObject risk = new JSONObject();
			risk.put("type", WalletConstant.WALLET_TYPE_BALANCE);
			risk.put("biz_no", biz_no);
			risk.put("change_amount", balance > 0 ? balance : -balance);
			risk.put("change_direction",
					balance > 0 ? WalletConstant.WALLET_DIRET_IN : WalletConstant.WALLET_DIRET_OUT);
			risk.put("trx_code", balance > 0 ? WalletConstant.PLATFORM_RECORD_TYPE_RISKIN
					: WalletConstant.PLATFORM_RECORD_TYPE_RISKOUT);
			risk.put("trade_no", trade_no);
			risk.put("biz_type", biz_type);
			risk.put("income_id", income_id);
			risk.put("biz_time", biz_time);
			record.add(risk);
			// 平台账户生成一条流水，风控入账/出账
			JSONObject inacc = new JSONObject();
			inacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
			inacc.put("biz_no", biz_no);
			inacc.put("change_amount", balance > 0 ? balance : -balance);
			inacc.put("change_direction",
					balance > 0 ? WalletConstant.WALLET_DIRET_OUT : WalletConstant.WALLET_DIRET_IN);
			inacc.put("trx_code", balance > 0 ? WalletConstant.PLATFORM_RECORD_TYPE_RISKOUT
					: WalletConstant.PLATFORM_RECORD_TYPE_RISKIN);
			inacc.put("biz_time", biz_time);
			platform.add(inacc);
			doctor.put("doc_id", doc_id);
			doctor.put("records", record);
			require.put("doctor", doctor);
			require.put("platform", platform);
			require.put("msgId", msgId);
			require.put("biz_time", biz_time);
			// 调用账户服务-事务记账
			bizWalletService.WalletRecordRisk(require.toJSONString(), String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("风控记账-分析订单金额组成异常", e);
		}

	}

	@Override
	public void transactionDone(TransactionDoneMessage message) throws WalletBizRecordException {
		try {
			if (message == null) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数message");
			}
			String doc_id = message.getData().get("doc_id").toString();
			String biz_no = message.getData().get("biz_no").toString();
			BigDecimal balance = new BigDecimal(message.getData().get("balance").toString());
			Long biz_time = StringUtils.isEmpty(message.getData().get("biz_time").toString())
					? System.currentTimeMillis() : Long.valueOf(message.getData().get("biz_time").toString());
			String biz_type = message.getData().get("biz_type").toString();
			String income_id = message.getData().get("income_id").toString();
			String trade_no = message.getData().get("trade_num").toString();
			String income_details = message.getData().get("income_details").toString();
			String msgId = message.getId();
			// 组合记账需求
			JSONObject require = new JSONObject();
			// 平台记账
			JSONArray platform = new JSONArray();
			// 医生端记账
			JSONObject doctor = new JSONObject();
			JSONArray record = new JSONArray();

			if (StringUtils.isNotEmpty(income_details)) {
				JSONArray jarr = JSONArray.parseArray(income_details);
				Iterator<Object> it = jarr.iterator();
				while (it.hasNext()) {
					JSONObject detail = (JSONObject) it.next();
					int price = detail.getIntValue("price");
					String detail_type = detail.getString("type");
					String detail_id = detail.getString("_id");
					// 开始记账
					// 医生账户生成一条流水，入账
					JSONObject inacc = new JSONObject();
					inacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
					inacc.put("biz_no", biz_no);
					inacc.put("change_amount", price);
					inacc.put("change_direction", WalletConstant.WALLET_DIRET_IN);
					inacc.put("trx_code", WalletConstant.DOCTOR_RECORD_TYPE_IN);
					inacc.put("trade_no", trade_no);
					inacc.put("biz_type", biz_type);
					inacc.put("income_id", income_id);
					inacc.put("detail_type", detail_type);
					inacc.put("detail_id", detail_id);
					inacc.put("occurTime", biz_time);
					record.add(inacc);
					// 平台账户生成一条流水，出账
					JSONObject outacc = new JSONObject();
					outacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
					outacc.put("biz_no", biz_no);
					outacc.put("change_amount", price);
					outacc.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
					outacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_OUT);
					outacc.put("occurTime", biz_time);
					platform.add(outacc);
					doctor.put("doc_id", doc_id);
					doctor.put("records", record);
					require.put("doctor", doctor);
					require.put("platform", platform);
					require.put("msgId", msgId);
					require.put("biz_time", biz_time);
				}
			} else {
				// 开始记账
				// 医生账户生成一条流水，入账
				JSONObject inacc = new JSONObject();
				inacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
				inacc.put("biz_no", biz_no);
				inacc.put("change_amount", balance.intValue());
				inacc.put("change_direction", WalletConstant.WALLET_DIRET_IN);
				inacc.put("trx_code", WalletConstant.DOCTOR_RECORD_TYPE_IN);
				inacc.put("trade_no", trade_no);
				inacc.put("biz_type", biz_type);
				inacc.put("income_id", income_id);
				inacc.put("occurTime", biz_time);
				record.add(inacc);
				// 平台账户生成一条流水，出账
				JSONObject outacc = new JSONObject();
				outacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
				outacc.put("biz_no", biz_no);
				outacc.put("change_amount", balance.intValue());
				outacc.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
				outacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_OUT);
				outacc.put("occurTime", biz_time);
				platform.add(outacc);
				doctor.put("doc_id", doc_id);
				doctor.put("records", record);
				require.put("doctor", doctor);
				require.put("platform", platform);
				require.put("msgId", msgId);
				require.put("biz_time", biz_time);
			}
			// 调用账户服务-事务记账
			bizWalletService.WalletRecordTransactionDone(require.toJSONString(), String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("交易完成记账-分析订单金额组成异常", e);
		}
	}

	@Override
	public void dkSuccess(String biz_no, String doc_id, int balance, Long biz_time, String msgId)
			throws WalletBizRecordException {
		try {
			if (StringUtils.isEmpty(doc_id)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数doc_id");
			}
			if (StringUtils.isEmpty(biz_no)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数biz_no");
			}
			// 组合记账需求
			JSONObject require = new JSONObject();
			// 医生端记账
			JSONObject doctor = new JSONObject();
			JSONArray record = new JSONArray();
			// 医生账户生成一条流水，出账
			JSONObject outacc = new JSONObject();
			outacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
			outacc.put("biz_no", biz_no);
			outacc.put("change_amount", balance);
			outacc.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
			outacc.put("trx_code", WalletConstant.DOCTOR_RECORD_TYPE_WITHDRAWAL);
			outacc.put("biz_time", biz_time);
			record.add(outacc);

			doctor.put("doc_id", doc_id);
			doctor.put("records", record);
			require.put("doctor", doctor);
			require.put("msgId", msgId);
			require.put("biz_time", biz_time);
			// 调用账户服务-事务记账
			bizWalletService.WalletRecordDkSuccess(require.toJSONString(), String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("打款成功记账-分析订单金额组成异常", e);
		}
	}

	@Override
	public void bizFrozen(String biz_no, String doc_id, int balance, long date, String msgId)
			throws WalletBizRecordException {
		try {
			if (StringUtils.isEmpty(doc_id)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数doc_id");
			}
			if (StringUtils.isEmpty(biz_no)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数biz_no");
			}
			// 组合记账需求
			JSONObject require = new JSONObject();
			// 平台记账
			JSONArray platform = new JSONArray();
			// 医生端记账
			JSONObject doctor = new JSONObject();
			JSONArray record = new JSONArray();
			// 医生账户生成一条流水，入账
			JSONObject inacc = new JSONObject();
			inacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
			inacc.put("biz_no", biz_no);
			inacc.put("change_amount", balance);
			inacc.put("change_direction", WalletConstant.WALLET_DIRET_IN);
			inacc.put("trx_code", WalletConstant.DOCTOR_RECORD_TYPE_IN);
			inacc.put("date", date);
			record.add(inacc);
			// 医生账户生成一条流水，冻结
			JSONObject frozen = new JSONObject();
			frozen.put("type", WalletConstant.WALLET_TYPE_BALANCE);
			frozen.put("biz_no", biz_no);
			frozen.put("change_amount", balance);
			frozen.put("change_direction", WalletConstant.WALLET_DIRET_IN);
			frozen.put("trx_code", WalletConstant.DOCTOR_RECORD_TYPE_FROZEN);
			frozen.put("date", date);
			record.add(frozen);
			// 平台账户生成一条流水，出账
			JSONObject outacc = new JSONObject();
			outacc.put("type", WalletConstant.WALLET_TYPE_BALANCE);
			outacc.put("biz_no", biz_no);
			outacc.put("change_amount", balance);
			outacc.put("change_direction", WalletConstant.WALLET_DIRET_OUT);
			outacc.put("trx_code", WalletConstant.PLATFORM_RECORD_TYPE_OUT);
			platform.add(outacc);
			doctor.put("doc_id", doc_id);
			doctor.put("records", record);
			require.put("doctor", doctor);
			require.put("platform", platform);
			require.put("msgId", msgId);
			// 调用账户服务-事务记账
			bizWalletService.WalletRecordFrozen(require.toJSONString(), String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("医生账户冻结金额记账-分析订单金额组成异常", e);
		}
	}

	@Override
	public void bizUnFrozen(String biz_no, String doc_id, int balance, String msgId) throws WalletBizRecordException {
		// TODO Auto-generated method stub

	}

	@Override
	public void dkFailure(String biz_no, String doc_id, int balance, Long biz_time, String msgId)
			throws WalletBizRecordException {
		try {
			if (StringUtils.isEmpty(doc_id)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数doc_id");
			}
			// 调用账户服务-事务记账
			bizWalletService.WalletRecordDkFailure(biz_no, balance, doc_id, msgId, String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("打款消息失败记账-异常", e);
		}
	}

	@Override
	public void setWithdrawalCan(String biz_no, String doc_id, int balance, Long biz_time, String msgId)
			throws WalletBizRecordException {
		try {
			if (StringUtils.isEmpty(doc_id)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数doc_id");
			}
			//调用账户服务-设置可提现金额
			bizWalletService.WalletSetWithdrawalCan(balance, doc_id, msgId, String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("设置可提现金额-异常", e);
		}
	}

	@Override
	public void setWithdrawalIng(String biz_no, String doc_id, int balance, Long biz_time, String msgId)
			throws WalletBizRecordException {
		try {
			if (StringUtils.isEmpty(doc_id)) {
				throw new WalletBizRecordException(WalletBizRecordException.MISSING_ARGS, "缺少参数doc_id");
			}
			//调用账户服务-设置可提现金额
			bizWalletService.WalletSetWithdrawalIng(balance, doc_id, msgId, String.valueOf(Math.random()));
		} catch (WalletBizException e) {
			logger.info("调用账户服务-事务记账异常", e);
		} catch (Exception e) {
			logger.info("设置提现中金额-异常", e);
		}
		
	}
}
